package com.jobguard.entity;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;

/**
 * function: restful api 返回对象
 * author: xxxxsl
 * date: 2024/12/3 19:45
 */
public record RestBean<T>(int code, T data, String message) {

    public static <T> RestBean<T> success(T data) {
        return new RestBean<>(200, data, "success");
    }

    public static <T> RestBean<T> success() {
        return new RestBean<>(200, null, "success");
    }

    public static <T> RestBean<T> unauthorized(String message) {
        return new RestBean<>(401, null, message);
    }

    public static <T> RestBean<T> forbidden(String message) {
        return new RestBean<>(403, null, message);
    }

    public static <T> RestBean<T> failure(int code, String message) {
        return new RestBean<>(code, null, message);
    }

    public String asJsonString() {
        return JSONObject.toJSONString(this, JSONWriter.Feature.WriteNulls);
    }
}
